// pages/identity/identity.js
import {
  uploadFile
} from '../../api/request'
import {
  saveCardAuth,
  getCardAuthentication
} from '../../api/identity'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyFlag: false,
    imageList: [
      "/assets/images/identity/id_1.png",
      "/assets/images/identity/id_2.png",
      "/assets/images/identity/id_3.png"
    ],
    uploadImageList: [
      '',
      '',
      ''
    ],
    phone:'',
    checked:false
  },
  jump(e){
    wx.navigateTo({
      url: '/pages/agreement/agreement?url='+'https://user-doc.yihaoyunche.com/management.html',
    })
  },
  onChange(){
    this.setData({
      checked:!this.data.checked
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  selectImage(e) {
    var that = this;
    var flag = e.currentTarget.dataset.flag;
    console.log(flag)
    wx.chooseImage({
      count: 1,
      fail: e => {
        console.log(e,'sss')
      },
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        that.data.imageList[flag] = tempFilePaths[0];
        that.setData({
          imageList: that.data.imageList
        })
      }
    })
  },
  apply() {
  
    if (this.data.imageList[0] == "/assets/images/identity/id_1.png") return wx.showToast({
      title: '身份证正面未上传',
      icon: 'none'
    })
    if (this.data.imageList[1] == "/assets/images/identity/id_2.png") return wx.showToast({
      title: '身份证反面未上传',
      icon: 'none'
    })
    if (this.data.imageList[2] == "/assets/images/identity/id_3.png") return wx.showToast({
      title: '驾驶证未上传',
      icon: 'none'
    })
    // if(!this.data.checked){
    //   return wx.showToast({
    //     title: '请阅读并同意信息查阅授权书',
    //     icon: 'none'
    //   })
    // }
    wx.showLoading({
      title: '上传中...',
    })
    // wx.showLoading({
    //   title: "加载中..."
    // })
    // vehicleCard({
    //   data:{
    //     imgFaceCardUrl:this.data.imageList[0],
    //     imgBackCardUrl:this.data.imageList[1],
    //     imgDriverLicenseUrl:this.data.imageList[2],
    //   }
    // })
    var num = 0;
    this.data.imageList.forEach((e, i) => {
      uploadFile({
        url: "mini/credential",
        filePath: e,
        name: 'file',
        type:i+1
      }).then(e => {
        var res = JSON.parse(e.data)
        num += 1;
        this.data.uploadImageList[i] = res.name;
        if (num == 3) {
          if(res.code == 401){
            wx.showToast({
              title: "登录过期",
              icon: 'none'
            })
            setTimeout(()=>{
              wx.navigateTo({
                url: '/pages/login/login',
              })
            },1000)
            
            return
          }
          this.examine()
        }
      }).catch(e => {
        this.data.applyFlag = false;
      })
    })


  },
  examine() {
    if (this.data.applyFlag) return wx.showToast({
      title: "请勿重复点击",
      icon: 'none'
    })
    this.data.applyFlag = true;
    wx.hideLoading()
    wx.showToast({
      title: '上传成功',
    })
    setTimeout(e => {
      wx.switchTab({
        url: '/pages/my/my',
      })
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})